package com.young.mylib;

import android.content.Context;
import android.content.SharedPreferences;
import android.text.TextUtils;
import android.util.Log;
import android.widget.Toast;

import com.young.mylib.utils.FastJsonUtils;

import java.util.Calendar;

/**
 * Created by Young on 2017/8/13.
 */

public class DebugLook {
    private final static String TAG = "Yetech_DebugLook";
    private static Context context;
    private static DebugLook instance;
    private static boolean isDebug = true;
    private SharedPreferences sharedPreferences;
    private SharedPreferences.Editor editor;

    public DebugLook(Context context) {
        this.context = context;
        this.sharedPreferences = context.getSharedPreferences("DebugInfo", Context.MODE_PRIVATE);
        this.editor = sharedPreferences.edit();
        config();
    }

    public static DebugLook getInstance() {
        if (instance == null) {
            instance = new DebugLook(context);
        }
        return instance;
    }

    private void config() {
        if (!isDebug) {
            return;
        }
        try{
            String str =sharedPreferences.getString(TAG,"");
            if(!TextUtils.isEmpty(str)){
                return;
            }

            DebugTime dt = new DebugTime();
            dt.setSetupTimeLong(System.currentTimeMillis());
            dt.setTimeLong(get30Day());
            String json = FastJsonUtils.toJSONString(dt);
            // Log.i(TAG,str);
            editor.putString(TAG, json);
            editor.commit();
        }catch (Exception e){

        }

    }


    public boolean isDebug() {
        if (!isDebug) {
            return false;
        }
        try {
            String str =sharedPreferences.getString(TAG,"");
            DebugTime dt = FastJsonUtils.toBean(str, DebugTime.class);
            long time = System.currentTimeMillis();

            if (time > dt.getTimeLong()) {
                Log.i(TAG,"isDebug ");
                Toast.makeText(context,"请联系APP开发者！",Toast.LENGTH_LONG).show();
                return true;
            }
        } catch (Exception e) {
            Log.i(TAG,"is not Debug ");
            return false;
        }
        Log.i(TAG,"is not Debug ");
        return false;
    }

    private long get30Day() {
        Calendar c = Calendar.getInstance(); // 当时的日期和时间
        int day; // 需要更改的天数
        day = c.get(Calendar.DAY_OF_MONTH) + 30;
        c.set(Calendar.DAY_OF_MONTH, day);
        return c.getTimeInMillis();
    }

    public static final String DEFAULT_DATE_TIME_FORMAT = "yyyy-MM-dd HH:mm:ss";

}
